package com.dfa.nypl.main;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import com.dfa.nypl.common.WebService;
import com.dfa.nypl.entity.ObjectUtils;
import com.dfa.nypl.tellafriend.TellAFriend;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.telephony.TelephonyManager;
import android.view.Window;

public class SplashScreen extends Activity {
	/** Called when the activity is first created. */
	protected boolean _active = true;
	protected int _splashTime = 3300;

	private TelephonyManager telephonyManager;
	private WakeLock lock;
	private ConnectivityManager connectivityManager;

	private boolean isRedirectActivity = true;
	private Handler mHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWindowParameters();
		setContentView(R.layout.splash_screen);

//		connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//
//		telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
//
//		initWakeLock();

		Thread th = new Thread() {
			@Override
			public void run() {
				try {
					int waited = 0;
					while (_active && (waited <= _splashTime)) {
						sleep(100);
						if (_active) {
							waited += 100;
						}

					}
//					if (!isConnected()) {
//						isRedirectActivity = false;
//					} else {
//						isRedirectActivity = true;
//						ObjectUtils.campaignInfo = WebService
//								.getSingletonService().getCampaignInfo();
//						ObjectUtils.quoteInfo = WebService
//								.getSingletonService().getRandomQuote();
//					}

				} catch (Exception e) {
					// do nothing
				} finally {
					
					Intent intent = new Intent(SplashScreen.this,
							TabHomeScreen.class);
					startActivity(intent);
					SplashScreen.this.finish();
					stop();
//					
//					if (isRedirectActivity) {
//						Intent intent = new Intent(SplashScreen.this,
//								TabHomeScreen.class);
//						startActivity(intent);
//						SplashScreen.this.finish();
//						stop();
//					} else {
//						mHandler.post(new Runnable() {
//
//							public void run() {
//								showAlertErrorNetworkConnect("The network connection failed");
//							}
//						});
//					}
				}
			}
		};
		th.start();
	}

	@Override
	protected void onPause() {
		super.onPause();
		//lock.release();
	}

	@Override
	protected void onResume() {
		super.onResume();
		//lock.acquire();

	}

	private void initWakeLock() {
		PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
		lock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK
				| PowerManager.ON_AFTER_RELEASE, "SPLASHSCREEN");
	}

	private boolean isConnected() {
		if (telephonyManager == null || connectivityManager == null) {
			return false;
		}

		final boolean roaming = telephonyManager.isNetworkRoaming();
		final NetworkInfo info = connectivityManager.getActiveNetworkInfo();

		return info != null && info.isConnected() && !roaming;
	}

	public void initWindowParameters() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
	}

	private void showAlertErrorNetworkConnect(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(SplashScreen.this);
		builder.setMessage(message).setTitle("Network").setPositiveButton("Ok",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface d, int which) {
						d.dismiss();
						SplashScreen.this.finish();
					}
				});
		builder.create().show();
	}
}